package com.ebupt.migu.music.bannertwo.controller;

import java.util.Arrays;import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ebupt.migu.music.bannertwo.entity.BannerTwoDetail;
import com.ebupt.migu.music.bannertwo.service.IBannerTwoDetailService;
import com.ebupt.migu.music.common.entity.ResultObject;
import lombok.extern.slf4j.Slf4j;


import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/bannerTwoDetail")
@Slf4j
public class BannerTwoDetailController {
	@Autowired
	private IBannerTwoDetailService bannerTwoDetailService;
	
	/**
	 * 分页列表查询
	 *
	 * @param bannerTwoDetail
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "banner_two_detail-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<BannerTwoDetail>> queryPageList(BannerTwoDetail bannerTwoDetail,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//		QueryWrapper<BannerTwoDetail> queryWrapper = QueryGenerator.initQueryWrapper(bannerTwoDetail, req.getParameterMap());
//		Page<BannerTwoDetail> page = new Page<BannerTwoDetail>(pageNo, pageSize);
//		IPage<BannerTwoDetail> pageList = bannerTwoDetailService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}
	
	/**
	 *   添加
	 *
	 * @param bannerTwoDetail
	 * @return
	 */
	@PostMapping(value = "/add")
	public ResultObject<String> add(@RequestBody BannerTwoDetail bannerTwoDetail) {
		bannerTwoDetailService.save(bannerTwoDetail);
		return ResultObject.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param bannerTwoDetail
	 * @return
	 */
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public ResultObject<String> edit(@RequestBody BannerTwoDetail bannerTwoDetail) {
		bannerTwoDetailService.updateById(bannerTwoDetail);
		return ResultObject.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public ResultObject<String> delete(@RequestParam(name="id",required=true) String id) {
		bannerTwoDetailService.removeById(id);
		return ResultObject.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public ResultObject<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bannerTwoDetailService.removeByIds(Arrays.asList(ids.split(",")));
		return ResultObject.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public ResultObject<BannerTwoDetail> queryById(@RequestParam(name="id",required=true) String id) {
		BannerTwoDetail bannerTwoDetail = bannerTwoDetailService.getById(id);
		if(bannerTwoDetail==null) {
			return ResultObject.error("未找到对应数据");
		}
		return ResultObject.ok(bannerTwoDetail);
	}
	/**
	 * 通过bannerid查询
	 *
	 * @param bannerid
	 * @return
	 */

	@GetMapping(value = "/queryByBannerId")
	public ResultObject<?> queryByBannerId(@RequestParam(name="bannerid",required=true) String bannerid) {
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("banner_id",bannerid);
	    List list = bannerTwoDetailService.list(wrapper);
		if(list==null) {
			return ResultObject.error("未找到对应数据");
		}
		return ResultObject.ok(list);
	}


}
